//本地数据

var app = getApp();
var page
var listsize
Page({

  data: {
    tabs: [
      {
        index: 0,
        name: '全部订单',
        selected: 1,
      }, {
        index: 1,
        name: '待付款',
      }, {
        index: 2,
        name: '待确认',
      }, {
        index: 3,
        name: '租赁中',
      }, {
        index: 4,
        name: '已完成',
      }
    ],
    imgurl: app.globalData.imgurl,
    options: [
      {
        status: 0,
        btns: [
          {
            text: '马上付款',
            event: 'pay'
          }, {
            text: '取消',
            event: 'cancel'
          }
        ]
      }, {
        status: 1,
        btns: []
      }, {
        status: 2,
        btns: []
      }, {
        status: 3,
        btns: []
      }
    ]
  },
  // tab切换
  switchTab: function (e) {
    var o = e.currentTarget.dataset
    var tabs = this.data.tabs
    for (var i = 0; i < tabs.length; i++) {
      tabs[i].selected = 0
    }
    tabs[o.index].selected = 1;
    if (o.index > 0) {
      this.getList({ status: o.index - 1 })
    } else {
      this.getAllList()
    }
    this.setData({
      tabs: tabs
    })
  },
  onLoad: function (e) {
  },
  onShow() {
    this.getAllList()
  },

  //全部订单列表
  getAllList: function (e) {
    var that = this
    wx.request({
      url: app.globalData.url + 'shuianddian_list?openid=' + wx.getStorageSync('openId'),
      success: function (res) {
        var orderlist = res.data
        that.setOrderlist(orderlist)
      }
    })
  },
  // 订单列表
  getList: function (e) {
    var that = this
    wx.request({
      url: app.globalData.url + 'member_list_list?type=1&zhuangtai=' + e.status + '&openid=' + wx.getStorageSync('openId'),
      success: function (res) {
        var orderlist = res.data
        that.setOrderlist(orderlist)
      }
    })
  },
  // 渲染订单列表
  setOrderlist: function (orderlist) {
    var that = this
    for (var i = 0; i < orderlist.length; i++) {
      orderlist[i].priceset = that.getprice(orderlist[i])
      orderlist[i].btns = that.getbtns(orderlist[i].type)
    }
    that.setData({
      orderlist: orderlist
    })
  },
  getbtns: function (status) {
    var options = this.data.options
    var btns
    switch (status) {
      case '0':
        btns = options[0].btns
        break;
      case '1':
        btns = options[1].btns
        break;
      case '2':
        btns = options[2].btns
        break;
      case '3':
        btns = options[3].btns
        break;
    }
    return btns
  },
  // 计算总价
  getprice(e) {
    var payPrice = Number(e.dingjin) + Number(e.yajin)
    var totalPrice = e.number * e.zi[0].cuxiao_price
    return {
      payPrice: payPrice,
      totalPrice: totalPrice
    }
  },
  // 付款
  pay: function (e) {
    var that = this
    var o = e.currentTarget.dataset
    wx.request({
      url: app.globalData.payurl + 'openid=' + wx.getStorageSync('openId') + '&order_id=' + o.id,
      success: function (res) {
        var payData = res.data
        if (payData.timeStamp) {
          wx.requestPayment({
            timeStamp: payData.timeStamp,
            nonceStr: payData.nonceStr,
            package: payData.package,
            signType: payData.signType,
            paySign: payData.paySign,
            success: function (res) {
              console.log(res)
            },
            fail: function (res) {
              console.log('支付失败')
            },
            complete: function (res) {

            },
          })
        }
      },
      fail: res => {
        console.log('错误')
      }
    })
  },
  detail: function (e) {
    var o = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/order/detail/orderdetail?id=' + o.id,
    })
  }

})